package com.shengyuan.manage_os.old.ying.erpm.util;

import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.R;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;

public class SoundControler {
	
	//public static SoundPool soundpool; 
	//private int mSoundSourceId;
	//private float volumnRatio;
	private MediaPlayer player; 
	private Vibrator vibrator;
	public SoundControler(Context context) {

//        float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//        float volumnCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//        volumnRatio = volumnCurrent / audioMaxVolumn;
        
//        if (soundpool==null) {
//        	soundpool = new SoundPool(1, AudioManager.STREAM_MUSIC, 5);
//		}
	}
	
	public void Load() {
		//mSoundSourceId = soundpool.load(ECApplication.Instance, R.raw.daka, 1);
	}
	
	public void Play(Context context) {
		//if (player==null) {
			player = MediaPlayer.create(context, R.raw.playend);
		//}
			try {
				player.prepare();
			} catch (Exception e) {
				e.printStackTrace();
			}
			//player.setLooping(true);
		player.start();
		
		vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);   

		long[] pattern = {800, 100, 400, 50}; // OFF/ON/OFF/ON...   

		vibrator.vibrate(pattern, 2);
	}
	
	public void Play( String arg0) {
		if (player==null) {
			player = new MediaPlayer();
		}
		try {
			player.setDataSource(arg0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//soundpool.play( mSoundSourceId, volumnRatio, volumnRatio, 0, 1,  1f);
		AudioManager am = (AudioManager) MainApplication.Instance.getSystemService(Context.AUDIO_SERVICE);
		if (am.getStreamVolume(AudioManager.STREAM_ALARM)!=0) {
			player.setAudioStreamType( AudioManager.STREAM_ALARM );
			player.setLooping(true);
			try {
				player.prepare();
			} catch (Exception e) {
				e.printStackTrace();
			}
			player.start();
		}
	}
	
	public void Stop()
	{
//		if (mSoundSourceId>0) {
//			soundpool.stop(mSoundSourceId);
//		}
		if (player!=null) {
			player.stop();
		}
		if (vibrator!=null) {
			vibrator.cancel();
		}
	}
	
	public void Unload(){
//		soundpool.unload(mSoundSourceId);
//		soundpool.release();
	}
	
}
